package com.bawei.a2010douyin.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bawei.a2010douyin.R;

public class LikeView extends RelativeLayout {
    public LikeView(Context context) {
        super(context);
    }

    public LikeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LikeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LikeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    //*********判断是否为双击
    private long lastTime;//上一次点击的时间
    public boolean isDoubleClick(){
        long currentTime = System.currentTimeMillis();//当前时间
        if(currentTime - lastTime <1000){//双击
            return true;
        }
        lastTime = currentTime;//更新上一次时间
        return false;
    }
    //默认是不拦截
    //双击：拦截
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            if(isDoubleClick()){//双击：拦截--》自己消费ontouchEvent
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();//动作类型：DOWN 按下 MOVE 移动 UP 抬起 CANCEL 取消
        float x = event.getX();
        float y = event.getY();
        if (action == MotionEvent.ACTION_DOWN){
                //1：创建ImageView并添加到当前容器中
                ImageView imageView = new ImageView(getContext());
                imageView.setImageResource(R.drawable.ic_xihuanred);//设置图片资源
                LayoutParams layoutParams = new LayoutParams(100, 100);//参数：宽度和高度
                layoutParams.leftMargin = (int) x-50;//左边距
                layoutParams.topMargin = (int) y-50;//上边距
                imageView.setLayoutParams(layoutParams);//设置参数
                addView(imageView);
                //2：动画
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "ScaleX", 1, 2);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "ScaleY", 1, 2);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "Alpha", 1, 0);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(2000);
                animatorSet.playTogether(scaleX,scaleY,alpha);
                animatorSet.start();
                //3:动画结束溢出view
                animatorSet.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        removeView(imageView);
                    }
                });



        }


        return true;
    }
}
